package com.ruoyi.product.service.impl;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.constant.HttpStatus;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.product.service.ISysBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.stereotype.Service;
import com.ruoyi.product.mapper.SysBrandMapper;
import com.ruoyi.product.domain.SysBrand;

/**
 * 品牌管理Service业务层处理
 * 
 * @author tsh
 * @date 2025-08-02
 */
@Service
public class SysBrandServiceImpl extends ServiceImpl<SysBrandMapper,SysBrand> implements ISysBrandService
{
    @Autowired
    private SysBrandMapper sysBrandMapper;

    @Autowired
    KafkaTemplate kafkaTemplate;
    @Autowired
    ElasticsearchRestTemplate elasticsearchRestTemplate;
    /**
     * 查询品牌管理
     * 
     * @param brandId 品牌管理主键
     * @return 品牌管理
     */
    @Override
    public SysBrand selectSysBrandByBrandId(Integer brandId)
    {

        return sysBrandMapper.selectSysBrandByBrandId(brandId);
    }

    /**
     * 查询品牌管理列表
     * 
     * @param sysBrand 品牌管理
     * @return 品牌管理
     */
    @Override
    public List<SysBrand> selectSysBrandList(SysBrand sysBrand)
    {
        return sysBrandMapper.selectSysBrandList(sysBrand);
    }

    /**
     * 新增品牌管理
     * 
     * @param sysBrand 品牌管理
     * @return 结果
     */
    @Override
    public int insertSysBrand(SysBrand sysBrand)
    {
        sysBrand.setCreateTime(DateUtils.getNowDate());

        String message= JSON.toJSONString(sysBrand);
        kafkaTemplate.send("topic-ruoyi",message);

        return sysBrandMapper.insertSysBrand(sysBrand);
    }

    @KafkaListener(topics = "topic-ruoyi")
    public void receiveMessage(String message, Acknowledgment acknowledgment){
        System.out.println("kafka接收到消息:"+message);
        acknowledgment.acknowledge();
    }

    /**
     * 修改品牌管理
     * 
     * @param sysBrand 品牌管理
     * @return 结果
     */
    @Override
    public int updateSysBrand(SysBrand sysBrand)
    {

        sysBrand.setUpdateTime(DateUtils.getNowDate());
        return sysBrandMapper.updateSysBrand(sysBrand);
    }

    /**
     * 批量删除品牌管理
     * 
     * @param brandIds 需要删除的品牌管理主键
     * @return 结果
     */
    @Override
    public int deleteSysBrandByBrandIds(Integer[] brandIds)
    {

        return sysBrandMapper.deleteSysBrandByBrandIds(brandIds);
    }

    /**
     * 删除品牌管理信息
     * 
     * @param brandId 品牌管理主键
     * @return 结果
     */
    @Override
    public int deleteSysBrandByBrandId(Integer brandId)
    {
        return sysBrandMapper.deleteSysBrandByBrandId(brandId);
    }

    @Override
    public TableDataInfo getBrandList(SysBrand sysBrand, Integer pageNum, Integer pageSize) {


        Page<SysBrand> page = new Page<>(pageNum, pageSize);
        Page<SysBrand> sysBrandPage = sysBrandMapper.selectPage(page, null);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setRows(sysBrandPage.getRecords());
        rspData.setTotal(sysBrandPage.getTotal());
        rspData.setMsg("查询成功");
        return rspData;
    }

    @Override
    public AjaxResult getBrandListAll() {
        List<SysBrand> list = sysBrandMapper.selectSysBrandList(null);
        return AjaxResult.success(list);
    }

}
